package com.lineage.server.model.map;

import com.lineage.MapReader;
import com.lineage.TextMapReader;
import com.lineage.config.Config;
import com.lineage.server.utils.PerformanceTimer;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1WorldMap {
  private static final Log _log = LogFactory.getLog(TextMapReader.class);
  
  private static L1WorldMap _instance;
  
  private Map<Integer, L1Map> _maps;
  
  public static L1WorldMap get() {
    if (_instance == null)
      _instance = new L1WorldMap(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    _log.info("MAP进行数据加载...");
    MapReader in = null;
    try {
      TextMapReader textMapReader = null;
      if (Config.CACHE_MAP_FILES) {
        in = new CachedMapReader();
      } else {
        textMapReader = new TextMapReader();
      } 
      this._maps = in.read();
      if (this._maps == null) {
        _log.error("MAP数据载入异常 maps未建立初始化");
        System.exit(0);
        return;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      _log.info("MAP数据加载完成(" + timer.get() + " ms)");
    } 
  }
  
  public L1Map getMap(short mapId) {
    L1Map map = this._maps.get(Integer.valueOf(mapId));
    if (map == null)
      map = L1Map.newNull(); 
    return map;
  }
  
  public void cloneMap(int targetId, int newId) {
    L1Map copymap = null;
    copymap = ((L1Map)this._maps.get(Integer.valueOf(targetId))).copyMap(newId);
    this._maps.put(Integer.valueOf(newId), copymap);
  }
}
